<?php

/*
 * B端操作功能模块
 * 功能：订单
 */

namespace Home\Controller;

use Think\B_Controller;

class OrderController extends B_Controller {
    
    public function __construct() {
        parent::__construct();
        $this->order = D('Order','Logic');
    }
    
    public function index(){

        $type = (int)$_GET['type'];
        $action_id = (int)$_GET['id'];
        $search = $_GET['Search'];
        $store_id = (int)$_GET['store_id'];
        $barber_id = (int)$_GET['barber_id'];
        
        if(!in_array($store_id,$this->store_id)){
            $store_id = $this->store_id;
        }
        
        if($action_id>0){
            $action = $this->a->getAction($action_id);
            if(!in_array($action['store_id'],(array)$store_id)){
                $this->_msg('_HAVE_NO_LEGAL_POWER_');
            }
            $store_id = $action['store_id'];
            $orders = $this->order->getActionOrderByActionId($action_id,0,$search,$barber_id,$type-1);
            $num = $this->order->countsByActionId($action_id,$search,$barber_id,$type-1);
        }else{
            $orders = $this->order->getActionOrderByStoreId($store_id,0,$search,$barber_id,$type-1);
            $num = $this->order->counts((array)$store_id,$search,$barber_id,$type-1);
        }
        $b = D('Barber','Logic');
        foreach($orders as $k=>$v){
            $barber = $b->getBarber($v['barber_id']);
            $orders[$k]['barber'] = $barber['name'];
            $people_num = $this->order->getTeamNum($v['team_number']);
            $_action = $this->a->getAction($v['action_id']);
            if($people_num['num']>2 || (time()-$v['create_time']>172800)){
                $orders[$k]['option'] = true;
            }
            $teamLeader= $this->order->getTeamLeader($v['team_number'],$v['client_user']);
            $orders[$k]['team']=$teamLeader['type'];
            $orders[$k]['model_type']=$_action['model_type'];
            $t=explode(',', $v['type']);
            $shop = D('Shopitem','Logic');
            foreach($t as $kk=>$vv){
                if($vv==''){
                    continue;
                }
                 if(is_numeric($vv)){
                        $item = $shop->getItemById($vv,$v['store_id'],true);
                        $item_name[] = $item['name'];
                }else{
                        $itm_name[] = $vv;
                }
            }
            $orders[$k]['type']=implode('+',$item_name);
            $item_name=array();
        }


        $barbers = $b->getBarberByStoreIds((array)$store_id);
        
        $this->assign('num',$num);
        $this->assign('barber_id',$barber_id);
        $this->assign('barbers',$barbers);
        $this->assign('search',$search);
        $this->assign('action_id',$action_id);
        $this->assign('action_name',$action['action_name']);
        $this->assign('store_id',$store_id);
        $this->assign('type',$type);
        $this->assign('orders',$orders);
        $this->display('order');
    }
    
    public function append(){
        $type = (int)$_GET['type'];
        $start = (int)$_GET['start'];
        $action_id = $_GET['id'];
        $search = $_GET['Search'];
        $store_id = (int)$_GET['store_id'];
        $barber_id = (int)$_GET['barber_id']; 
        
        if(!in_array($store_id,$this->store_id)){
            $store_id = $this->store_id;
        }
        
        if($action_id>0){
            $action = $this->a->getAction($action_id);
            if(!in_array($action['store_id'],(array)$store_id)){
                $this->_msg('_HAVE_NO_LEGAL_POWER_');
            }
            $store_id = $action['store_id'];
            $orders = $this->order->getActionOrderByActionId($action_id,$start,$search,$barber_id,$type-1);
        }else{
            $orders = $this->order->getActionOrderByStoreId((array)$store_id,$start,$search,$barber_id,$type-1);
        }
        if(count($orders)>0){
            $b = D('Barber','Logic');
            foreach($orders as $k=>$v){
                $orders[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                $orders[$k]['price'] = $v['price']/100;
                $barber = $b->getBarber($v['barber_id']);
                $orders[$k]['barber'] = $barber['name'];
                $people_num = $this->order->getTeamNum($v['team_number']);
                $_action = $this->a->getAction($v['action_id']);
                if($people_num['num']>2 || (time()-$v['create_time']>172800)){
                    $orders[$k]['option'] = 'true';
                }
                $teamLeader= $this->order->getTeamLeader($v['team_number'],$v['client_user']);
                $orders[$k]['team']=$teamLeader['type'];
                $orders[$k]['model_type']=$_action['model_type'];
                $t=explode(',', $v['type']);
                $shop = D('Shopitem','Logic');
                foreach($t as $kk=>$vv){
                    if($vv==''){
                        continue;
                    }
                     if(is_numeric($vv)){
                            $item = $shop->getItemById($vv,$v['store_id'],true);
                            $item_name[] = $item['name'];
                    }else{
                            $itm_name[] = $vv;
                    }
                }
                $orders[$k]['type']=implode('+',$item_name);
                $item_name=array();
               
            }
            exit(json_encode($orders));
        }
        exit('false');
    }
    
    /*订单详情*/
    public function info(){
        //发送短信用
        $_SESSION['s_verify'] = time();

        $order_id = (int)$_GET['order_id'];
        $order = $this->order->getOrderById($order_id);

        if(!in_array($order['store_id'],$this->store_id)){
            $this->_msg('_HAVE_NO_LEGAL_POWER_');
        }
        $oinfo = D('Orderinfo',"Logic");
        $order_info = $oinfo->getOrderInfo($order_id,$order['store_id']);

        $this->assign('order',$order);
        $this->assign('order_info',$order_info);
        $this->display('info');
    }
    
    /*订单详情中消费一次操作*/
    public function subtract(){
        $order_id = (int)$_GET['order_id'];
        $order_info_id = (int)$_GET['order_info_id'];
        $oinfo = D('Orderinfo','Logic');
        $order_info = $oinfo->getOrderInfoById($order_info_id,$order_id);

        if(!in_array($order_info['store_id'],$this->store_id)){
            $this->log($order_id,0,'fail');
            exit("false");
        }
        $oinfo = D('Orderinfo',"Logic");
        $re = $oinfo->updateNumber($order_info_id,$order_info['order_id'],$order_info['count']-1);
        
        if($re){
            $this->send_Msg($order_info,$order_id,1);
            $this->log($order_id,0,'success');

            exit('true');
        }else{
            $this->log($order_id,0,'fail');
            exit('false');
        }
    }
    
    /*订单详情中撤销一次操作*/
    public function add(){
        $order_id = (int)$_GET['order_id'];
        $order_info_id = (int)$_GET['order_info_id'];
        $oinfo = D('Orderinfo','Logic');
        $order_info = $oinfo->getOrderInfoById($order_info_id,$order_id);

        if(!in_array($order_info['store_id'],$this->store_id)){
            $this->log($order_id,1,'fail');
            exit("false");
        }
        $oinfo = D('Orderinfo',"Logic");

        if($order_info['count'] >= $order_info['number']){
            $this->log($order_id,1,'fail');
            exit('false');
        }
        $re = $oinfo->updateNumber($order_info_id,$order_info['order_id'],$order_info['count']+1);
        if($re){
            $this->send_Msg($order_info,$order_id,2);
            $this->log($order_id,1,'success');
            exit('true');
        }else{
            $this->log($order_id,1,'fail');
            exit('false');
        }
    }

    /*订单完成操作*/
    public function update(){
        $id = (int)$_GET['id'];
        $type = array('status'=>4); //订单完成后 进入待评价 
        $order = $this->order->getOrderById($id);
        if($order['id']<1 || !in_array($order['store_id'],$this->store_id)){
            $this->_msg('_HAVE_NO_LEGAL_POWER_');
        }
        $oinfo = D('Orderinfo','Logic');
        $order_info = $oinfo->getOrderInfo($id,$order['store_id']);
        foreach($order_info as $v){
            if($v['count']>0){
                $this->_msg('还有未完成的订单子项！！');
            }
        }
        $re = $this->order->updateStatus($id,$type);
        if($re === false){
            $this->_msg('_SYSTEM_ERROR_');
        }else{
            redirect('/home/order?type='.(int)$_GET['type'].'&store_id='.(int)$_GET['store_id']."&barber_id=".(int)$_GET['barber_id'].'&Search='.urlencode(trim($_GET['Search'])));
        }
    }

    protected function log($order_id,$type,$status){
        //log
        import('Logs', 'Log');
        $mongo = new \Log\Logs();
        $mongo->operation_Log($order_id,$type,$status);

    }

    //msg 
    protected function send_Msg($order_info,$order_id,$type){
        //发送短信
        $shop = D('Shopitem','Logic');
        $item = $shop->getItemById($order_info['item_id'],$order_info['store_id'],true);
        $s = D('store','Logic');
        $store = $s->getStore($order_info['store_id']);
        $order = $this->order->getOrderById($order_id);
        import('MsgCode', 'Msgcode');
        $m = new \Msgcode\MsgCode();
        $time = date('Y-m-d H:i:s',time());
        $mobile = $order['mobile'];
        if($type==1){
            //订单消费一次操作
            $_contents = '4&1&'.$time.'&'.$store['name'].'&'.$item['name'].'&'.$store['tel']; 
       }elseif($type==2){
            //订单撤销一次操作
            $_contents = '8&1&'.$time.'&'.$store['name'].'&'.$item['name'].'&'.$store['tel']; 
       }
        $res = $m->sendMsg_b($mobile,$_contents,true);
    }

}
